/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.chenzehe.zdmya.action;
import static com.chenzehe.zdmya.Constants.SESSION_USER;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.chenzehe.java.core.utils.WebUtil;
import com.chenzehe.zdmya.domain.Collection;
import com.chenzehe.zdmya.domain.OperatePoint;
import com.chenzehe.zdmya.domain.User;
import com.chenzehe.zdmya.service.ICollectionService;
import com.chenzehe.zdmya.service.IUserService;

/**
 * @description
 * 
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-8-14 下午08:14:05
 */
@Controller
public class CollectionAction extends BaseAction {
	@Resource
	ICollectionService collectionService;
	@Resource
	IUserService userService;

	@RequestMapping(value = "/collection/save.html")
	@ResponseBody
	public String saveCollection(HttpServletRequest request) {
		try {
			Collection collection = new Collection();

			User user = (User) WebUtils.getSessionAttribute(request,
					SESSION_USER);
			if (user == null) {
				return "nologin";
			}
			int collectionId = WebUtil.getInt(request, "post_id", 0);
			int type = WebUtil.getInt(request, "type", 0);
			if (collectionId == 0) {
				return "error";
			}
			collection.setContentId(collectionId);
			collection.setType(type);
			if (collectionService.queryByUserAndCollectionId(user.getId(),
					collectionId, type) != null) {
				return "exists";
			}

			collection.setIssueTime(new Date());

			collection.setUserId(user.getId());
			collectionService.save(collection);
			userService.operatePoint(user.getId(), OperatePoint.COLLECTION);
			return "ok";

		} catch (Exception e) {
			return "error";
		}

	}
}
